#!/bin/bash
#set -x
#******************************************************************************
# @file    : entrypoint.sh
# @author  : simon
# @date    : 2018-08-28 15:18:43
#
# @brief   : entry point for manage service start order
# history  : init
#******************************************************************************

: ${SLEEP_SECOND:=1}

wait_for() {
    echo Waiting for $1 to listen on $2...
    wget --spider --connect-timeout=3 $1:$2 > /dev/null 2>&1
    echo "wget8761==$?"
    while [ $? -ne 0 ]
    do
      echo waiting...$?; sleep $SLEEP_SECOND;
      wget --spider --connect-timeout=3 $1:$2 > /dev/null 2>&1
      echo "wget8761==$?"
    done
}

declare DEPENDS
declare CMD

while getopts "d:a:" arg
do
    case $arg in
        d)
            DEPENDS=$OPTARG
            ;;
        a)
            DEPENDS=$OPTARG,$DEPEND_OPTS
            ;;
        ?)
            echo "use default depend "
            DEPENDS=$DEPEND_OPTS
            ;;
    esac
done

CMD="java $JAVA_OPTS $CMD_OPTS"
echo "$DEPENDS $CMD"

for var in ${DEPENDS//,/}
do
    host=${var%:*}
    port=${var#*:}
    wait_for $host $port
done

eval $CMD